package com.example.demo.poc.example;

import com.jayway.jsonpath.JsonPath;
import org.apache.commons.text.StringEscapeUtils;

import java.io.File;
import java.io.IOException;

public class JayWayJsonpathTest {
    public static void main(String[] args) {
//        AviatorEvaluator.addFunction(new AviatorSumFunction());
//        AviatorEvaluator.addFunction(new AviatorAVGFunction());
////        AviatorEvaluator.addFunction(new AviatorMaxFunction());
//
//        Map<String,Object> m = new HashMap<>();
//        m.put("d",d);
//
//        System.out.println(AviatorEvaluator.execute("avg(d)",m));

//        Double[] d = {222.75,222.73,220.98,233.47,230.77,230.62,223.99,237.21,239.18,233.93,235.79,228.35,226.54,227.39,221.59,233.34,236.65,224.49,224.94,221.54,229.92,239.93,222.36,227.25,221.07,222.71,221.98,233.29,234.79,237.8,230.47,226.56,236.28,239.53,237.62,221.25,238.41,224.69,232.77,230.15,232.26,238.43,232.46,229.6,232.97,230.98,223.55,228.72,237.49,228.23,231.22,238.41,236.26,224.25,225.92,220.63,225.41,228.76,228.82,223.57,231.46,227.65,239.87,233.24,225.11,239.47,237.9,236.97,226.15,236.17,226.94,239.53,226.06,220.84,228.12,237.14,220.32,220.79,221.52,240.24,227.03,238.77,227.02,240.28,233.71,230.56,238.83,221.28,239.16,221.83,232.56,221.63,222.08,222.49,229.52,236.6,233.18,221.37,227.28,237.3,232.82,229.44,230.9,226.42,220.68,224.56,224.12,236.45,229.53,235.79,224.81,220.88,239.66,230.32,233.88,233.86,235.9,231.09,232.16,239.04,234.08,238.11,224.28,232.89,228.55,231.54,234.08,233.31,237.69,239.63,220.71,228.86,229.2,239.41,229.74,229.27,231.88,223.42,238.62,222.78,225.84,230.29,220.76,230.81,227.74,222.34,237.5,235.51,233.1,238.28,232.32,220.78,222.22,233.92,226.56,232.27,221.76,229.07,220.89,226.92};

//        try {
//            System.out.println(engine.eval("avg(d)"));
//        } catch (ScriptException e) {
//            e.printStackTrace();
//        }


        File f = new File("C:\\Users\\shink\\OneDrive\\文档\\jetext.json");
        String t = null;
        try {
            t = org.apache.commons.io.FileUtils.readFileToString(f,"UTF8");
            t = StringEscapeUtils.unescapeJava(t);
        } catch (IOException e) {
            e.printStackTrace();
        }
        boolean isValidJson = false;
//        JSONValidator validator = JSONValidator.from(t);
//        isValidJson = validator.validate();
        System.out.println(isValidJson);

        String jsonPath = "$[*][*]['id','name']";
//        String jsonPath = "$[0][0]['id']";


        Object o = JsonPath.read(t,jsonPath);
        System.out.println(o.getClass());
//        List<Object> d = JSONArrayToObjectArray((JSONArray) o);
//        System.out.println(d.size());
//        for(Object s: d){
//            System.out.println(s);
//        }

        o = JsonPath.parse(t).read(jsonPath);
        String str = o.toString();
        System.out.println(str);




//        ScriptEngineManager manager = new ScriptEngineManager();
//        ScriptEngine engine = manager.getEngineByName("AviatorScript");
//        engine.put("d",d);

//        try {
//            System.out.println(engine.eval("avg(d)"));
//        } catch (ScriptException e) {
//            e.printStackTrace();
//        }
    }





}
